package com.tuanzi.loan.business.repository.sms.impl;

import com.tuanzi.loan.business.entity.sms.SmsSendTaskStatus;
import com.tuanzi.loan.business.repository.sms.custom.SmsSendTaskRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.vo.request.family.SmsCondition;
import com.tuanzi.loan.vo.request.family.SmsSendTaskSearchRequest;
import com.tuanzi.loan.vo.response.family.SmsSendTaskSearchResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSmsCalculateTask.LOAN_SMS_CALCULATE_TASK;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSmsSendTask.LOAN_SMS_SEND_TASK;

/**
 * @author Ray 2017/09/21
 */
@SuppressWarnings({ "rawtypes" })
public class SmsSendTaskRepositoryImpl extends JOOQBaseRepository implements SmsSendTaskRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_SMS_SEND_TASK;
	}

	@Override
	public Page<SmsSendTaskSearchResponse> list(SmsSendTaskSearchRequest searchRequest, Pageable pageable) {
		fields(LOAN_SMS_CALCULATE_TASK.NAME, LOAN_SMS_SEND_TASK.ID, LOAN_SMS_SEND_TASK.CONTENT, LOAN_SMS_SEND_TASK.STATUS, LOAN_SMS_CALCULATE_TASK.PHONE_COUNT,
				LOAN_SMS_SEND_TASK.COST, LOAN_SMS_SEND_TASK.EXECUTE_TIME, LOAN_SMS_SEND_TASK.PLATFORM_CHANNEL, LOAN_SMS_SEND_TASK.REGISTER_COUNT, LOAN_SMS_SEND_TASK.LOAN_COUNT,
				LOAN_SMS_SEND_TASK.CREATE_TIME, LOAN_SMS_SEND_TASK.INCOME, LOAN_SMS_SEND_TASK.REMARK, LOAN_SMS_SEND_TASK.SHORT_LINK,LOAN_SMS_CALCULATE_TASK.TASK_CONDITION);

		leftJoin(LOAN_SMS_CALCULATE_TASK, LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID.eq(LOAN_SMS_CALCULATE_TASK.ID));

		contains(LOAN_SMS_CALCULATE_TASK.NAME, searchRequest.getName());
		ge(LOAN_SMS_SEND_TASK.EXECUTE_TIME, searchRequest.getExecuteTimeStart());
		le(LOAN_SMS_SEND_TASK.EXECUTE_TIME, searchRequest.getExecuteTimeEnd());
		ge(LOAN_SMS_CALCULATE_TASK.PHONE_COUNT, searchRequest.getCountStart());
		le(LOAN_SMS_CALCULATE_TASK.PHONE_COUNT, searchRequest.getCountEnd());
		contains(LOAN_SMS_SEND_TASK.CONTENT, searchRequest.getContent());
		eq(LOAN_SMS_SEND_TASK.STATUS, searchRequest.getStatus());
		eq(LOAN_SMS_SEND_TASK.PLATFORM_CHANNEL, searchRequest.getPlatformChannel());
		neq(LOAN_SMS_SEND_TASK.STATUS, SmsSendTaskStatus.WAITING_SUBMIT.ordinal());
		orderBy(LOAN_SMS_SEND_TASK.CREATE_TIME, Sort.Direction.DESC);
		return search(pageable, new SmsTaskHistorySearchResponseRecordMapper());
	}

	private final class SmsTaskHistorySearchResponseRecordMapper implements RecordMapper<Record, SmsSendTaskSearchResponse> {

		@Override
		public SmsSendTaskSearchResponse map(Record record) {

			/*Map<String,String> telecomOperators = new HashMap<String,String>();
			telecomOperators.put("0","移动");
			telecomOperators.put("1","联通");
			telecomOperators.put("2","电信");*/
			String condition = record.get(LOAN_SMS_CALCULATE_TASK.TASK_CONDITION);
			SmsCondition smsCondition = new SmsCondition();
			if(condition != null && StringUtils.isNotBlank(condition)){
				smsCondition = GsonUtils.toObject(condition, SmsCondition.class);
			}
			SmsSendTaskSearchResponse response = new SmsSendTaskSearchResponse();
			response.setName(record.get(LOAN_SMS_CALCULATE_TASK.NAME));
			response.setId(record.get(LOAN_SMS_SEND_TASK.ID));
			response.setContent(record.get(LOAN_SMS_SEND_TASK.CONTENT));
			response.setShortLink(record.get(LOAN_SMS_SEND_TASK.SHORT_LINK));
			response.setStatus(record.get(LOAN_SMS_SEND_TASK.STATUS));
			//response.setCost(record.get(LOAN_SMS_SEND_TASK.COST) == null ? 0D : record.get(LOAN_SMS_SEND_TASK.COST).doubleValue());
			response.setExecuteTime(record.get(LOAN_SMS_SEND_TASK.EXECUTE_TIME));
			response.setPlatformChannel(record.get(LOAN_SMS_SEND_TASK.PLATFORM_CHANNEL));
			response.setCount(record.get(LOAN_SMS_CALCULATE_TASK.PHONE_COUNT));
			//response.setRegisterCount(record.get(LOAN_SMS_SEND_TASK.REGISTER_COUNT));
			//response.setLoanCount(record.get(LOAN_SMS_SEND_TASK.LOAN_COUNT));
			response.setCreateTime(record.get(LOAN_SMS_SEND_TASK.CREATE_TIME));
			//response.setIncome(Objects.isNull(record.get(LOAN_SMS_SEND_TASK.INCOME)) ? null : record.get(LOAN_SMS_SEND_TASK.INCOME).floatValue());
			//response.setRemark(record.get(LOAN_SMS_SEND_TASK.REMARK));
			//response.setUserTypes(smsCondition.getUserTypes() == null || smsCondition.getUserTypes().isEmpty()? "":smsCondition.getUserTypes().get(0));
			response.setUserTypes(smsCondition.getUserTypes());
			response.setTelecomOperators(smsCondition.getTelecomOperators());
			return response;
		}
	}

	@Override
	public List<String> findDistinctFirstPartyNames() {
		return dslContext.selectDistinct(LOAN_SMS_SEND_TASK.FIRST_PARTY_NAME).from(LOAN_SMS_SEND_TASK).
				where(LOAN_SMS_SEND_TASK.FIRST_PARTY_NAME.isNotNull()).and(LOAN_SMS_SEND_TASK.FIRST_PARTY_NAME.notEqual(""))
				.fetch(LOAN_SMS_SEND_TASK.FIRST_PARTY_NAME);
	}

	@Override
	public List<Long> findDistinctCalculateTaskIdByFirstPartyNames(List<String> firstPartyNames) {
		return dslContext.selectDistinct(LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID).from(LOAN_SMS_SEND_TASK)
				.where(LOAN_SMS_SEND_TASK.FIRST_PARTY_NAME.in(firstPartyNames))
				.and(LOAN_SMS_SEND_TASK.STATUS.eq(SmsSendTaskStatus.SEND_DONE.ordinal()))
				.fetch(LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID);
	}

	@Override
	public List<Long> findDistinctCalculateTaskIdByFirstPartyNamesAndExecuteTimeAfter(Date executeDate, List<String> firstPartyNames) {
		return dslContext.selectDistinct(LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID).from(LOAN_SMS_SEND_TASK)
				.where(LOAN_SMS_SEND_TASK.EXECUTE_TIME.ge(Timestamp.from(executeDate.toInstant())))
				.and(LOAN_SMS_SEND_TASK.FIRST_PARTY_NAME.in(firstPartyNames))
				.and(LOAN_SMS_SEND_TASK.STATUS.eq(SmsSendTaskStatus.SEND_DONE.ordinal()))
				.fetch(LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID);
	}

	@Override
	public List<Long> findDistinctCalculateTaskIdByStatusAndExecuteTimeAfter(SmsSendTaskStatus status,
			Date executeDate) {
		return dslContext.selectDistinct(LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID).from(LOAN_SMS_SEND_TASK)
				.where(LOAN_SMS_SEND_TASK.EXECUTE_TIME.ge(Timestamp.from(executeDate.toInstant())))
				.and(LOAN_SMS_SEND_TASK.STATUS.eq(SmsSendTaskStatus.SEND_DONE.ordinal()))
				.fetch(LOAN_SMS_SEND_TASK.CALCULATE_TASK_ID);
	}

}
